package com.audi.core.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.springframework.transaction.annotation.Transactional;

import com.audi.core.common.Page;
import com.audi.core.dao.BaseDAO;
import com.audi.core.dao.PropertyFilter;
import com.audi.core.dao.PropertyFilter.MatchType;
import com.audi.core.service.BaseService;


public class BaseServiceImpl<T, PK extends Serializable> implements BaseService<T,PK> {	
	
    private BaseDAO<T,PK> baseDAO;

    public void setBaseDAO(BaseDAO<T,PK> baseDAO) {
        this.baseDAO = baseDAO;
    }
    protected BaseDAO<T,PK> getBaseDAO() {
        return this.baseDAO;
    }
    public T save(T entity){
        return baseDAO.save(entity);
    }
    
    public void deleteById(PK id){
        baseDAO.delete(id);
    }
    public void delete(final T entity){
        baseDAO.delete(entity);
    }
    
    public T update(T entity){
        return baseDAO.update(entity);
    }

    public T saveOrUpdate(T entity){
        return baseDAO.update(entity);
    }

    public T merge(T entity){
        return baseDAO.merge(entity);
    }
 
    @Transactional(readOnly=true)
    public T getById(PK id){
        return baseDAO.get(id);
    }
    
    @Transactional(readOnly=true)
    public List<T> getAll() {
        return baseDAO.getAll();
    }
    
    /**
     * 分页查询
     */
    @Override
    @Transactional(readOnly=true)
    public Page<T> getAll(final Page<T> page) {
        return baseDAO.getAll(page);
    }
    /**
     * 分页查询
     */
    @Transactional(readOnly=true)
    public Page<T> findPage(final Page<T> page, final String hql, final Object... values){
        return baseDAO.findPage(page, hql, values);
    }
    
    @Transactional(readOnly=true)
    public Criterion buildCriterion(String propertyName,Object propertyValue, MatchType matchType) {
        return baseDAO.buildCriterion(propertyName, propertyValue, matchType);
    }

    @Transactional(readOnly=true)
    public Criterion[] buildPropertyFilterCriterions(
            List<PropertyFilter> filters) {
        
        return baseDAO.buildCriterionByPropertyFilter(filters);
    }

    @Transactional(readOnly=true)
    public Page<T> findPage(final Page<T> page, final String hql, final Map<String, ?> values){
        return baseDAO.findPage(page, hql, values);
    }
    
    @Transactional(readOnly=true)
    public Page<T> findPage(final Page<T> page, final Criterion... criterions){
        return baseDAO.findPage(page, criterions);
    }

    /**
     * 设置分页参数到Query对象,辅助函数.
     */
    @Transactional(readOnly=true)
    public Query setPageParameter(final Query q, final Page<T> page) {
        return baseDAO.setPageParameterToQuery(q, page);
    }
    
    /**
     * 设置分页参数到Criteria对象,辅助函数.
     */
    @Transactional(readOnly=true)
    public Criteria setPageParameter(final Criteria c, final Page<T> page){
        return baseDAO.setPageParameterToCriteria(c, page);
    }

    /**
     * 执行count查询获得本次Hql查询所能获得的对象总数.
     * 
     * 本函数只能自动处理简单的hql语句,复杂的hql查询请另行编写count语句查询.
     */
    @Transactional(readOnly=true)
    public long countHqlResult(final String hql, final Object... values){
        return baseDAO.countHqlResult(hql, values);
    }

    /**
     * 执行count查询获得本次Hql查询所能获得的对象总数.
     * 
     * 本函数只能自动处理简单的hql语句,复杂的hql查询请另行编写count语句查询.
     */
    @Transactional(readOnly=true)
    public long countHqlResult(final String hql, final Map<String, ?> values) {
        return baseDAO.countHqlResult(hql, values);
    }

    /**
     * 执行count查询获得本次Criteria查询所能获得的对象总数.
     */ 
    @Transactional(readOnly=true)
    public long countCriteriaResult(final Criteria c){
        return baseDAO.countCriteriaResult(c);
    }

    /**
     * 属性过滤条件(PropertyFilter)查询函数
     */
    @Transactional(readOnly=true)
    public List<T> findBy(final String propertyName, final Object value, final MatchType matchType){
        return baseDAO.findBy(propertyName, value, matchType);
    }

    @Transactional(readOnly=true)
    public List<T> find(List<PropertyFilter> filters) {
        return baseDAO.find(filters);
    }

    @Transactional(readOnly=true)
    public Page<T> findPage(final Page<T> page, final List<PropertyFilter> filters){
        return baseDAO.findPage(page, filters);
    }
    
    @Transactional(readOnly=true)
    public boolean isPropertyUnique(final String propertyName, final Object newValue, final Object oldValue){
        return baseDAO.isPropertyUnique(propertyName, newValue, oldValue);
    }
    
    
    @Override
    public List executeList(String sql, boolean cacheable) {
        
        return baseDAO.executeList(sql, cacheable);
    }
    @Override
    public List executeListByCondition(String sqlstr, Object[] params,
            boolean cacheable) {
        
        return baseDAO.executeListByCondition(sqlstr, params, cacheable);
    }
    
    
    @Override
    public List executeListByCondition(int begin, int pageSize, String sqlstr,
            Object[] params, boolean cacheable) {
        return baseDAO.executeListByCondition(begin, pageSize, sqlstr, params, cacheable);
    }
    @Override
    public Page<T> findByProperty(int pageNo, int pageSize, String hql,
            Object[] arr) {
        
        return baseDAO.findByProperty(pageNo, pageSize, hql, arr);
    }   
    
    /**
     * 跟据某个属性查找唯一值
     */
    public T findUniqueBy(final String propertyName, final Object value){
        return baseDAO.findUniqueBy(propertyName, value);
    }
    
    public void flush(){
        baseDAO.flush();
    }
    
    /**
     * 按Criteria查询唯一对象.
     * 
     * @param criterions 数量可变的Criterion.
     */
    public T findUnique(final Criterion... criterions){
        return baseDAO.findUnique(criterions);
    }
    
    /**
     * 执行HQL进行批量修改/删除操作.
     * 
     * @param values 数量可变的参数,按顺序绑定.
     * @return 更新记录数.
     */
    public int batchExecute(final String hql, final Object... values){
        return baseDAO.batchExecute(hql, values);
    }
    
    
    /**
     * 执行HQL进行批量修改/删除操作.
     * 
     * @param values 命名参数,按名称绑定.
     * @return 更新记录数.
     */
    public int batchExecute(final String hql, final Map<String, ?> values) {
        return baseDAO.batchExecute(hql, values);
    }
    
    
    
    /**
     * 按HQL查询唯一对象.
     * 
     * @param values 数量可变的参数,按顺序绑定.
     */
    public <X> X findUnique(final String hql, final Object... values) {
        return baseDAO.findUnique(hql, values);
    }
    
    //form S s where s.status=1
    public <X> X findUnique(String hql) {
        return baseDAO.findUnique(hql);
    }
    
    /**
     * 按HQL查询唯一对象.
     * 
     * @param values 命名参数,按名称绑定.
     */
    public <X> X findUnique(final String hql, final Map<String, ?> values){
        return baseDAO.findUnique(hql, values);
    }
    
    /**
     * 按属性查找对象列表, 匹配方式为相等.
     */
    public List<T> findBy(final String propertyName, final Object value) {
        return baseDAO.findBy(propertyName, value);
    }
    
    /**
     *  获取全部对象, 支持按属性行序.
     */
    public List<T> getAll(String orderByProperty, boolean isAsc){
        return baseDAO.getAll(orderByProperty,isAsc);
    }
    
    public Page<T> findPage(final Page<T> page){
        return baseDAO.getAll(page);
    }
    
    
    public Page<T> findPage(int pageNo,int pageSize, final String hql, final Object... values){
        Page<T> page = new Page<T>(pageNo,pageSize);
        return baseDAO.findPage(page, hql, values);
    }
    
    public Page<T> findPage(int pageNo,int pageSize, final String hql, final Map<String, ?> values){
        Page<T> page = new Page<T>(pageNo,pageSize);
        return baseDAO.findPage(page, hql, values);
    }   
    
    public Page<T> findPage(int pageNo,int pageSize, final Criterion... criterions){
        Page<T> page = new Page<T>(pageNo,pageSize);
        return baseDAO.findPage(page, criterions);
    }

    public Page<T> findPage(int pageNo,int pageSize){
        Page<T> page = new Page<T>(pageNo,pageSize);
        return baseDAO.findPage(page);
    }
    
    
    public Page executePage(Page page, String sql, boolean cacheable){
        return baseDAO.executePage(page, sql, cacheable);
    }
    /**
     * sql
     */
    public Page executePage(int begin, int pageSize, String sqlstr, final Object[] params, boolean cacheable){
        
        return baseDAO.executePage(begin, pageSize, sqlstr, params, cacheable);
    }
    /**
     * sql
     */
    public Page executePage(Page page, String sqlstr, final Object[] params, boolean cacheable){
        return baseDAO.executePage(page, sqlstr, params, cacheable);
    }
    
    
    public <X> List<X> find(final String hql, final Object... values){
        return baseDAO.find(hql, values);
    }

    /**
     * 按HQL查询对象列表.
     * 
     * @param values 命名参数,按名称绑定.
     */
    public <X> List<X> find(final String hql, final Map<String, ?> values){
        return baseDAO.find(hql, values);
        
    }
    
    
}